package com.stm.bi.utils;

import com.stm.bi.dto.vo.SubjectVO;
import com.stm.bi.enums.Unit;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class SubjectUtils {

    public static Object getDiyReportRandomValue(SubjectVO subject){
        if(subject == null){
            return getDiyReportRandomValue("0,1000", 1);
        }

        if(subject.getUnit() == null){
            if(subject.getName().contains("率") ){
                return getDiyReportRandomValue("0,1", 4);
            }

            if(subject.getName().contains("数")){
                return getDiyReportRandomValue("0,1000", 1);
            }
        }


        return getDiyReportRandomValue(subject.getRandomRange(), subject.getUnit());
    }

    /**
     * 获取随机值
     * @param randomRange 随机范围
     * @param unit        单位
     * @return 随机值
     */
    public static Object getDiyReportRandomValue(String randomRange, Integer unit){

        if(randomRange == null){
            randomRange = "0,1000";
        }

        BigDecimal value = BigDecimal.ZERO;
        if(randomRange.contains(",")){
            String[] randomRangeArr = randomRange.split(",");

            if(randomRangeArr.length == 2){
                int min = Integer.parseInt(randomRangeArr[0]);
                int max = Integer.parseInt(randomRangeArr[1]);
                value = BigDecimal.valueOf(min + (Math.random() * ((max - min))));
            }
        }

        return switch (Unit.getUnit(unit)) {
            case Unit.INT -> value.setScale(0, RoundingMode.HALF_UP);
            case Unit.TWO_DECIMAL -> value.setScale(2, RoundingMode.HALF_UP);
            case Unit.TEN_THOUSAND -> value.divide(BigDecimal.valueOf(10000), 2, RoundingMode.HALF_UP);
            case Unit.RATE ->
                    value.multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP) + "%";
            case null -> "-";
        };

    }

}
